//
//  AXFSearchFlowLayout.m
//  AXFEightGroup
//
//  Created by ooops on 12/27/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFSearchFlowLayout.h"


@implementation AXFSearchFlowLayout

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    //[super layoutAttributesForItemAtIndexPath:indexPath];
    //UIEdgeInsets defaultEdgeInsets = {0, 0, 0, 0};
    //创建布局属性
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    // collectionView的宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    CGFloat w = (collectionViewW - defaultEdgeInsets.left - defaultEdgeInsets.right - (defaultColumnCount - 1)*defaultColumnMargin)/defaultColumnCount;
    //CGFloat h = w *  1.5;//indexPath.section <= 3 ? 0.5 :
    CGFloat h = w *  1.5;//indexPath.section <= 3 ? 0.5 :
    CGFloat x = defaultEdgeInsets.left;
    CGFloat y = defaultEdgeInsets.top;
    
    //根据计算位置
    NSInteger section = indexPath.row / defaultColumnCount;
    NSInteger row = indexPath.row % defaultColumnCount;
    
    //    switch (indexPath.section) {
    //        case 0:
    //
    //            break;
    //
    //        default:
    //            break;
    //    }
    //
    y = section * ((h + defaultRowMargin)) + defaultEdgeInsets.top;
    x = defaultEdgeInsets.left + row * (w + defaultColumnMargin);
    
    //设置布局属性的frame
    y += self.sectionHeight;
    attributes.frame = CGRectMake(x, y + navBarHeight, w, h);
    
    self.maxSectionHeight = y + h;
    //下一组时 更新高度
    if (indexPath.row == [self.collectionView numberOfItemsInSection:indexPath.section]-1) {
        self.sectionHeight = self.maxSectionHeight + defaultEdgeInsets.bottom;
    }
    //获得最底部位置
    self.maxHeight = self.maxSectionHeight + defaultEdgeInsets.bottom;
    return attributes;
}


@end
